package cn.wolfcode.luowowo.service.impl;


import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
public class DestinationServiceImpl implements IDestinationService {



    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IDestinationService destinationService;


    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public void save(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public List<Destination> queryByRegionId(String rid) {

        Region region = regionService.get(rid);

        List<String> refIds = region.getRefIds();

        List<Destination> destinations =  destinationRepository.findByIdIn(refIds);

        return destinations;
    }

    @Override
    public Page<Destination> query(DestinationQuery qo, String parentId) {
        // 查询条件
        Query query = new Query();

        // 拼接高级查询条件
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(".*"+qo.getKeyword()+".*"));
        }

        if (StringUtils.hasLength(parentId)) {
            query.addCriteria(Criteria.where("parentId").is(parentId));
        }else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }


        // 列表数据
        PageRequest request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        return DBHelper.query(template,Destination.class,query,request);
    }

    @Override
    public List<Destination> getToastsInfo(List<Destination> list, String parentId) {

        if(!StringUtils.hasLength(parentId)){
            return list;
        }

        Destination destination = destinationService.get(parentId);
        list.add(destination);
        if( StringUtils.hasLength(destination.getParentId()) ){
            this.getToastsInfo(list,destination.getParentId());
        }
        // 翻转
        Collections.reverse(list);
        return list;


    }

    @Override
    public List<Destination> search(String regionId) {

        List<Destination> destinations = new ArrayList<>();

        if ("-1".equals(regionId)) {
            destinations = destinationRepository.findByParentName("中国");
        }else{
            destinations = destinationRepository.findByParentId(regionId);
        }
        for (Destination destination : destinations ){

            destination.setChildren(destinationRepository.findByParentId(destination.getId()));
        }
        return destinations;
    }

    @Override
    public Destination getByName(String keyword) {

        return destinationRepository.findByName(keyword);
    }


}
